<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Nginx Upstream 模块</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'nginx';
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            border-left: 4px solid #667eea;
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: translateX(5px);
            box-shadow: 0 10px 30px rgba(102, 126, 234, 0.1);
        }
        
        .mermaid {
            text-align: center;
            margin: 30px 0;
        }
        
        .highlight-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-server text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                深入理解 Nginx <span class="text-yellow-300">Upstream</span> 模块
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                掌握负载均衡的核心技术，构建高可用、高性能的分布式系统架构
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">N</span>ginx 的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">upstream</code> 模块是实现负载均衡和请求分发的核心组件。在互联网应用中，如何有效地将请求分配到多台后端服务器，保证系统的高可用性和性能，是我们必须面对的挑战。本文将深入探讨 upstream 模块的工作原理、配置方法和最佳实践。
            </p>
        </div>

        <!-- Architecture Diagram -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">系统架构概览</h2>
            <div class="mermaid">
                graph TB
                    Client[<i class='fas fa-users'></i> 客户端]
                    Nginx[<i class='fas fa-server'></i> Nginx]
                    
                    subgraph Upstream["<i class='fas fa-layer-group'></i> Upstream 服务器组"]
                        Server1[<i class='fas fa-server'></i> Server 1]
                        Server2[<i class='fas fa-server'></i> Server 2]
                        Server3[<i class='fas fa-server'></i> Server 3]
                    end
                    
                    Client -->|请求| Nginx
                    Nginx -->|负载均衡| Server1
                    Nginx -->|负载均衡| Server2
                    Nginx -->|负载均衡| Server3
                    
                    style Client fill:#f9f,stroke:#333,stroke-width:2px
                    style Nginx fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style Upstream fill:#f0f4f8,stroke:#667eea,stroke-width:2px
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-cube mr-3 text-purple-600"></i>核心概念
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="feature-card p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-balance-scale mr-2 text-purple-600"></i>负载均衡
                    </h3>
                    <p class="text-gray-600">平衡请求负载，防止单台服务器过载，提升系统整体性能和吞吐量。</p>
                </div>
                
                <div class="feature-card p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-shield-alt mr-2 text-purple-600"></i>高可用性
                    </h3>
                    <p class="text-gray-600">实现故障转移，确保系统在部分服务器故障时仍能正常工作。</p>
                </div>
                
                <div class="feature-card p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-expand-arrows-alt mr-2 text-purple-600"></i>可扩展性
                    </h3>
                    <p class="text-gray-600">通过增加服务器来扩展系统处理能力，支持业务增长。</p>
                </div>
                
                <div class="feature-card p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-heartbeat mr-2 text-purple-600"></i>健康检查
                    </h3>
                    <p class="text-gray-600">定期检测后端服务器的健康状态，确保请求只发送到健康的服务器。</p>
                </div>
            </div>
        </div>

        <!-- Configuration Example -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-code mr-3 text-purple-600"></i>配置示例
            </h2>
            
            <div class="mb-6">
                <h3 class="text-xl font-semibold mb-4 text-gray-700">基础配置</h3>
                <div class="code-block">
<pre><code>upstream backend {
    server backend1.demo.com;
    server backend2.demo.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}</code></pre>
                </div>
            </div>
            
            <div class="mb-6">
                <h3 class="text-xl font-semibold mb-4 text-gray-700">加权负载均衡</h3>
                <div class="code-block">
<pre><code>upstream my_app {
    server 192.168.1.1 weight=3;
    server 192.168.1.2 weight=2;
    server 192.168.1.3;
    server 192.168.1.4 backup;
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Load Balancing Algorithms -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-random mr-3 text-purple-600"></i>负载均衡算法
            </h2>
            
            <div class="space-y-6">
                <div class="bg-gradient-to-r from-purple-50 to-pink-50 p-6 rounded-lg border-l-4 border-purple-600">
                    <h3 class="text-xl font-semibold mb-2 text-gray-800">轮询（Round Robin）</h3>
                    <p class="text-gray-600 mb-3">默认算法，按顺序将请求分发到后端服务器。适用于服务器性能相当的情况。</p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle mr-2 text-green-500"></i>
                        <span>简单易用</span>
                        <i class="fas fa-times-circle ml-4 mr-2 text-red-500"></i>
                        <span>不考虑服务器负载</span>
                    </div>
                </div>
                
                <div class="bg-gradient-to-r from-blue-50 to-indigo-50 p-6 rounded-lg border-l-4 border-blue-600">
                    <h3 class="text-xl font-semibold mb-2 text-gray-800">最少连接（Least Connections）</h3>
                    <p class="text-gray-600 mb-3">将请求分配给当前连接数最少的服务器。适用于连接数差异较大的场景。</p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle mr-2 text-green-500"></i>
                        <span>动态负载均衡</span>
                        <i class="fas fa-check-circle ml-4 mr-2 text-green-500"></i>
                        <span>避免服务器过载</span>
                    </div>
                </div>
                
                <div class="bg-gradient-to-r from-green-50 to-teal-50 p-6 rounded-lg border-l-4 border-green-600">
                    <h3 class="text-xl font-semibold mb-2 text-gray-800">IP 哈希（IP Hash）</h3>
                    <p class="text-gray-600 mb-3">根据客户端 IP 地址进行哈希计算，确保同一客户端请求固定分配给某台服务器。</p>
                    <div class="flex items-center text-sm text-gray-500">
                        <i class="fas fa-check-circle mr-2 text-green-500"></i>
                        <span>会话保持</span>
                        <i class="fas fa-times-circle ml-4 mr-2 text-red-500"></i>
                        <span>负载可能不均</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Health Check & Failover -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-heartbeat mr-3 text-purple-600"></i>健康检查与故障转移
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-700">
                        <i class="fas fa-stethoscope mr-2 